using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BlogSystem.Ability.Docking.NewsApp;
using BlogSystem.Ability.Docking.NewsApp.Dto;
using BlogSystem.Domain.NewsInfo;
using Microsoft.EntityFrameworkCore;
using Youshow.Ace.Ability;
using Youshow.Ace.Domain.Repository;

namespace BlogSystem.Ability.NewsApp
{
    public class NewsServicer : AbilityServicer, INewsServicer
    {
        public IRepository<News> NewsRep {get; set;}
        public async Task<List<NewsDto>> GetNewsDtoListAsync(int pageIndex = 1, int pageSize = 1)
        {
            int sikpSize = (pageIndex - 1)*pageSize;
            var newsList = NewsRep.OrderByDescending(m => m.PublishTime).Skip(sikpSize).Take(pageSize).ToList();
            var newsDtos = ModelMapper.Map<List<News>, List<NewsDto>>(newsList);
            return newsDtos;

        }

        public async Task<int> GetTotalCountAsync()
        {
            return await NewsRep.CountAsync();
        }

        public async Task<NewsDto> GetNewsDtoAsync(int id)
        {
            var news = await NewsRep.GetAsync(m => m.Id == id);
            var newsDto = ModelMapper.Map<News, NewsDto>(news);
            return newsDto;
        }
    }
}